//
//  PublicViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/18.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "PublicViewController.h"
#import "PublicTableViewCell.h"
#import "AttentionPublicViewController.h"
@interface PublicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * publicTableView;
@property (nonatomic,strong)NSMutableArray * dataAry;
@end

@implementation PublicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"公众号";
    [self.view addSubview:self.publicTableView];
    [self getPublicList];
    _dataAry = [NSMutableArray array];
    // Do any additional setup after loading the view.
}

- (void)getPublicList{
    [_dataAry removeAllObjects];
    [RequestData PostPublicWithToken:[LoginManager shareLoginManager].userModel.token userUUid:[LoginManager shareLoginManager].userModel.uuid Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"公众号列表：%@",Dict);
        KWeakSelf;
        NSArray * ary = Dict[@"data"];
        for (NSDictionary * dic in ary) {
            PublicModel * model = [PublicModel mj_objectWithKeyValues:dic];
            [weakSelf.dataAry addObject:model];
        }
        [self.publicTableView reloadData];
    } failure:^(NSString * _Nonnull errMsg) {
        if (errMsg) {
            [self showTipHudWithText:errMsg];
        }else{
            [self showTipHudWithText:@"网络错误"];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataAry.count;
//    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63.f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PublicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PublicTableViewCell"];
    if (!cell) {
        cell = [[PublicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PublicTableViewCell" withCellType:@"PublicListCell"];
    }
    cell.cellType = @"PublicListCell";
    cell.indexPath = indexPath;
    cell.model = _dataAry[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AttentionPublicViewController * attentionVC = [[AttentionPublicViewController alloc]init];
    PublicModel * model = _dataAry[indexPath.row];
    attentionVC.udid = model.udid;
    [self.navigationController pushViewController:attentionVC animated:YES];
}

- (UITableView *)publicTableView{
    if (!_publicTableView) {
        _publicTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _publicTableView.delegate = self;
        _publicTableView.dataSource = self;
        _publicTableView.backgroundColor = [UIColor whiteColor];
        _publicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _publicTableView.tableHeaderView = [UIView new];
        _publicTableView.tableFooterView = [UIView new];
        _publicTableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _publicTableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
